{% extends "sysadmin/base.html" %}
{% load seahub_tags i18n %}
{% block cur_org %}tab-cur{% endblock %}

{% block left_panel %}{{block.super}}
<form action="{% url 'sys_org_search' %}" method="get" class="side-search-form">
    <input type="text" name="name" class="input" value="" placeholder="Search orgs by name..." />
</form>
{% endblock %}

{% block right_panel %}
<div class="tabnav ovhd">
    <ul class="tabnav-tabs fleft">
        <li class="tabnav-tab {% if all_page %}tabnav-tab-cur{% endif %}"><a href="{% url 'sys_org_admin' %}">{% trans "All" %}</a></li>
        {% if enable_org_plan %}
        <li class="tabnav-tab {% if paid_page %}tabnav-tab-cur{% endif %}"><a href="{% url 'sys_org_admin' %}?filter=paid">Paid</a></li>
        {% endif %}
    </ul>
    <button id="add-btn" class="fright">{% trans "Add organization" %}</button>
</div>

<form id="add-org-form" action="" method="post" class="hide">{% csrf_token %}
    <h3>{% trans "Add organization" %}</h3>
    <label for="id_name">{% trans "Name" %}</label><br />
    <input type="text" name="name" id="id_name" class="input" /><br />
    <label for="id_owner">{% trans "Owner" %}</label><span class="icon-question-sign" title="{% trans "Owner can use admin panel in an organization, must be a new account." %}" style="color:#666; margin-left:3px;"></span><br />
    <input type="text" name="owner" id="id_owner" class="input" /><br />
    <label for="id_password1">{% trans "Password" %}</label><br />
    <input type="password" name="password1" id="id_password1" class="input" /><br />    
    <label for="id_password2">{% trans "Confirm Password" %}</label><br />
    <input type="password" name="password2" id="id_password2" class="input" /><br />    

    <p class="error hide"></p>
    <button type="submit" class="submit">{% trans "Submit" %}</button>
</form>

{% if orgs %}

{% include "sysadmin/org_admin_table.html" %}

  {% if not hide_paginator %}
  {% include "snippets/admin_paginator.html" %}
  {% endif %}
{% else %}
<p>{% trans "None." %}</p>
{% endif %}

{% endblock %}

{% block extra_script %}
<script type="text/javascript">
$('#add-btn').on('click', function() {
    $('#add-org-form').modal();
    $('#simplemodal-container').css({'width':'auto', 'height':'auto'});
});

$('#add-org-form').on('submit', function() {
    var form = $(this),
        form_id = form.attr('id'),
        submit_btn = form.find('[type="submit"]'),
        name = $.trim(form.children('[name="name"]').val()),
        owner = $.trim(form.children('[name="owner"]').val()),
        pwd1 = $.trim(form.children('[name="password1"]').val()),
        pwd2 = $.trim(form.children('[name="password2"]').val());

    if (!name) {
        apply_form_error(form_id, "{% trans "Name can not be blank"%}");
        return false;
    }
    if (!owner) {
        apply_form_error(form_id, "{% trans "Owner can not be blank"%}");
        return false;
    }
    if (!pwd1) {
        apply_form_error(form_id, "{% trans "Password can not be blank" %}");
        return false;
    } 
    if (!pwd2) {
        apply_form_error(form_id, "{% trans "Please enter the password again" %}");
        return false;
    } 
    if (pwd1 != pwd2) {
        apply_form_error(form_id, "{% trans "Passwords do not match" %}");
        return false;
    } 

    disable(submit_btn);
    $.ajax({
        url: '{% url 'org_add' %}',
        type: 'POST',
        datatype: 'json',
        cache: false,
        beforeSend: prepareCSRFToken,
        data: {
            'org_name': name,
            'name': owner,
            'email': owner,
            'password1': pwd1,
            'password2': pwd2
        },
        success: function(data) {
            location.reload(true);
        },
        error: function(xhr, textStatus, errorThrown) {
            var error_msg = prepareAjaxErrorMsg(xhr);
            apply_form_error(form_id, error_msg);
            enable(submit_btn);
        }
    });

    return false;
});

addConfirmTo($('.unset-trial'), {
    'title':"{% trans "Remove Trial" %}",
    'con':"{% trans "Are you sure you want to remove trial for %s ?" %}"
});

{% include "sysadmin/snippets/org_admin_js.html" %}
</script>
{% endblock %}
